package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.datavisor.DataVisorData;
import com.ucredit.babel.common.model.third.datavisor.DataVisorRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * datavisor
 *
 * @author renzhanzheng
 */

public class DataVisorParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * datavisor
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     * @return
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        DataVisorRecord obj = (DataVisorRecord)model;
        DataVisorData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());

        StringBuilder sb = getBaseInfo(obj, successful);
        //响应类型
        sb.append(info.getString("responseType") == null ? "" : info.getString("responseType")).append(CRT001);
        //tierNumber
        sb.append(info.getString("tierNumber") == null ? "" : info.getString("tierNumber")).append(CRT001);
        //用户ID
        sb.append(info.getString("userID") == null ? "" : info.getString("userID")).append(CRT001);
        //clusterSize
        sb.append(info.getString("clusterSize") == null ? "" : info.getString("clusterSize")).append(CRT001);
        //欺诈分（0.0-1.0间）
        sb.append(info.getString("score") == null ? "" : info.getString("score")).append(CRT001);
        //feature
        sb.append(info.getString("feature_space_and_density") == null ? "" : info.getString("feature_space_and_density")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_datavisor_score_inc";
    }

    @Override
    public String getType() {
        return RecordType.DATAVISOR_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "datavisor";
    }
}
